<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class m_laporan_kunjungan extends CI_Model {
	var $table = "kunjungan";

	public function __construct(){
		parent::__construct();
	}

	public function get_all(){
		$sql = " SELECT * FROM $this->table k 
		JOIN pasien p ON k.KdPasien = p.KdPasien
					ORDER BY KdKunjungan DESC";

		return $sql;
	}

	public function search($input){
		$enddate = $input['keyword'][0]; 
		$startdate = $input['keyword'][1];

		if($startdate != "" && $enddate != ""){
					$sql = "
					SELECT * FROM $this->table k
					JOIN pasien p ON k.KdPasien = p.KdPasien
					WHERE Tanggal BETWEEN '$startdate' AND '$enddate'
					ORDER BY KdKunjungan DESC";
				}else if($startdate == "" && $enddate == ""){
					#apakah enddate dan startdate kosong
					$sql = "
					SELECT * FROM $this->table k 
					JOIN pasien p ON k.KdPasien = p.KdPasien
					ORDER BY KdKunjungan DESC";
				}else{
					if($startdate == ""){
						$sql = "
						SELECT * FROM $this->table k
					JOIN pasien p ON k.KdPasien = p.KdPasien
						WHERE Tanggal = '$enddate'
						ORDER BY KdKunjungan DESC";
					}else if($enddate == ""){
						$sql = "
						SELECT * FROM $this->table k
					JOIN pasien p ON k.KdPasien = p.KdPasien
						WHERE Tanggal = '$startdate'
						ORDER BY KdKunjungan DESC";
					}
				}

		return $sql;
	}

	public function export(){
		$this->db->join('pasien', 'pasien.KdPasien = kunjungan.KdPasien', 'left');
		$query = $this->db->get($this->table);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	public function export_by($filter, $keyword){
		if($filter == 'by_code'){
			if(is_numeric($keyword)){
				$this->db->join('pasien', 'pasien.KdPasien = kunjungan.KdPasien');
				$this->db->where('KdKunjungan', $keyword);
				$this->db->order_by('KdKunjungan', 'desc');
				$query = $this->db->get('kunjungan');				

				if($query->num_rows() > 0) {
					$result = $query->result_array();
					$query->free_result();
				}else{
					$result = array();
				}
				return $result;
			}else{
				set_flash('danger','Format keyword salah');
				redirect('laporan_kunjungan', 'refresh');
			}
		}else if($filter == 'by_name'){
			$this->db->join('pasien', 'pasien.KdPasien = kunjungan.KdPasien');
			$where = "Nama LIKE '%$keyword%'";
			$this->db->where($where);
			$this->db->order_by('KdKunjungan', 'desc');
			$query = $this->db->get('kunjungan');

			if($query->num_rows() > 0) {
				$result = $query->result_array();
				$query->free_result();
			}else{
				$result = array();
			}
			return $result;
		}else if($filter == 'by_keluhan'){
			$this->db->join('pasien', 'pasien.KdPasien = kunjungan.KdPasien');
			$where = "Keluhan LIKE '%$keyword%'";
			$this->db->where($where);
			$this->db->order_by('KdKunjungan', 'desc');
			$query = $this->db->get('kunjungan');

			if($query->num_rows() > 0) {
				$result = $query->result_array();
				$query->free_result();
			}else{
				$result = array();
			}
			return $result;

			$sql = "SELECT * FROM $this->table k
			JOIN pasien p ON k.KdPasien = p.KdPasien
			WHERE Informasi LIKE '%$keyword%'
			ORDER BY KdKunjungan DESC
			";
		}
	}
}

/* End of file m_laporan_kunjungan.php */
/* Location: ./application/models/m_laporan_kunjungan.php */